package com.time.sky.controller;

import com.alibaba.fastjson.JSON;
import com.time.sky.entity.User;
import com.time.sky.mapper.UserMapper;
import com.time.sky.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.HtmlUtils;

import java.util.HashMap;

@RestController
@ResponseBody
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService ;
    @RequestMapping("/login")
    public String getUserByUsername(@RequestBody User user){
        String username=user.getUsername();
        HashMap<String, Object> data = new HashMap<>();
        username= HtmlUtils.htmlEscape(username);
        User userByUsername = userService.getUserByName(username, user.getPassword());
        if(userByUsername==null){
            data.put("flag",400);
        }else{
            data.put("log",userByUsername);
            data.put("flag",200);
        }
        String s = JSON.toJSONString(data);
        return s;
    }

    @RequestMapping("updateUser")
    public void updateUser(User user) {
        System.out.println(user);
        userService.updateUser(user);
    }

}
